﻿Imports System.Net.Mail
Imports System.Data.OleDb

Public Class Email


    Private m_ChildFormNumber As Integer
    Private myMsg As New MailMessage
    Private smtpSender As New SmtpClient
    Dim username As String = ""
    Dim password As String = ""
    Dim b As Boolean = False
    Dim it As Boolean = False
    Dim un As Boolean = False
    Dim htmlHEADERString As String = ""
    Dim htmlBODYString As String = ""
    Dim htmlFOOTERString As String = ""
    Dim sthlh As Integer = 0
    Dim ArrayRecipients() As String
    Dim ArraysentAds() As String



#Region "Toolbar button click"

    'Handlers for the toolbar button click event

    Private Sub FontsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If FontDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            BodyText.Font = FontDialog1.Font
        End If

    End Sub

    Private Sub colorsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            BodyText.SelectionColor = ColorDialog1.Color
        End If
    End Sub


    Private Sub ItalicsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If it = False Then
            Dim italic As New Font(BodyText.Font, FontStyle.Italic)
            BodyText.Font = italic
            it = True
        Else
            Dim Norm As New Font(BodyText.Font, FontStyle.Regular)
            BodyText.Font = Norm
            it = False
        End If
    End Sub

    Private Sub UnderlineButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If un = False Then
            Dim Underline As New Font(BodyText.Font, FontStyle.Underline)
            BodyText.Font = Underline
            un = True
        Else
            Dim Norm As New Font(BodyText.Font, FontStyle.Regular)
            BodyText.Font = Norm
            un = False
        End If

    End Sub

    Private Sub left_alignment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        BodyText.SelectionAlignment = HorizontalAlignment.Left

    End Sub

    Private Sub center_alignment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        BodyText.SelectionAlignment = HorizontalAlignment.Center

    End Sub

    Private Sub right_alignment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        BodyText.SelectionAlignment = HorizontalAlignment.Right
    End Sub

    Private Sub CutToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (BodyText.SelectedText.Length > 0) Then
            BodyText.Cut()
        End If
    End Sub

    Private Sub CopyToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (BodyText.SelectedText.Length > 0) Then
            BodyText.Copy()
        End If
    End Sub

    Private Sub PasteToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            BodyText.Paste()
        Catch ex As Exception
            MessageBox.Show("Paste Failed")
        End Try
    End Sub
#End Region

    Private Sub ExitForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitForm.Click
        Me.Close()
    End Sub

#Region "Email"

    Private Sub createMessage()
        Dim ToArray() As String = Split(ToText.Text, ",")

        myMsg.From = New MailAddress(FromText.Text.Trim)

        If ToText.Text.Contains(",") Then

            For i As Integer = 0 To ToArray.Length - 1
                myMsg.To.Add(ToArray(i))
            Next
        Else
            myMsg.To.Add(ToText.Text.Trim)
        End If

        myMsg.Subject = SubjectText.Text
        myMsg.Body = PreView.DocumentText & BodyText.Text
    End Sub

    Private Sub createSMTP()
        smtpSender.Credentials = New Net.NetworkCredential(username, password)
        smtpSender.Host = "Smtp.live.com"
        smtpSender.Port = "587"
    End Sub

    Private Sub sendMessage()
        Try
            status.Text = ""
            smtpSender.Send(myMsg)
            MessageBox.Show("E-mail send succesfully")

            'update data base
            ' Dim SentMax As Integer = 0
            ' Dim strMax As String = "SELECT MAX(idsent) FROM Sent"

            ' Try
            '  Using cnn As New OleDbConnection(My.Settings.OurDataBaseConnectionString)
            'Using cmd As New OleDbCommand(strMax, cnn)
            'cnn.Open()
            ' For i As Integer = 0 To ArrayRecipients.Length - 1
            'For j As Integer = 0 To ArraysentAds.Length - 1
            'SentMax = cmd.ExecuteNonQuery()
            ' strsql = "INSERT INTO Sent (idsent,idR,idA) values ('" & SentMax + 1 & "','" & ArrayRecipients(i).ToString & "','" & ArraysentAds(j).ToString & "')"
            ' Next

            ' Next


            ' cnn.Close()
            ' End Using

            ' End Using

            ' Catch ex As Exception
            '  MessageBox.Show(ex.Message)
            'End Try


        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub sentMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sentMessage.Click
        createMessage()
        createSMTP()
        sendMessage()
    End Sub

#End Region

    Private Sub Email_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'OurDataBaseDataSet.GroupsAggelion' table. You can move, or remove it, as needed.
        Me.GroupsAggelionTableAdapter.Fill(Me.OurDataBaseDataSet.GroupsAggelion)
        'TODO: This line of code loads data into the 'OurDataBaseDataSet.Groups' table. You can move, or remove it, as needed.
        Me.GroupsTableAdapter.Fill(Me.OurDataBaseDataSet.Groups)

        Dim strSQL As String = "SELECT Username,Password FROM Users"

        Try
            Using cnn As New OleDbConnection(My.Settings.OurDataBaseConnectionString)
                Using cmd As New OleDbCommand(strSQL, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()
                        ' Loop through all the rows, retrieving the 
                        ' columns you need. Also look into the GetString
                        ' method (and other Get... methods) for a faster 
                        ' way to retrieve individual columns.

                        While dr.Read
                            username = dr("Username").ToString
                            password = dr("Password").ToString
                        End While
                    End Using
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        FromText.Text = username

        'Show Cities in Combo Box
        Dim strSQL2 As String = "SELECT DISTINCT City FROM Angelia"

        Try
            Using cnn As New OleDbConnection(My.Settings.OurDataBaseConnectionString)
                Using cmd As New OleDbCommand(strSQL2, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()
                        ' Loop through all the rows, retrieving the 
                        ' columns you need. Also look into the GetString
                        ' method (and other Get... methods) for a faster 
                        ' way to retrieve individual columns.

                        While dr.Read
                            JobsCity.Items.Add(dr("City").ToString)
                        End While
                    End Using
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

#Region "Html formation"

    Private Sub CreatehtmlHEADERString()

        'Style
        htmlHEADERString = htmlHEADERString & "<html xmlns='http://www.w3.org/1999/xhtml'><head><title>Jobs</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />"
        htmlHEADERString = htmlHEADERString & "</head><body>"
        htmlHEADERString = htmlHEADERString & "<table width='75%' border='0' align='center' cellpadding='0' cellspacing='0'>"
        htmlHEADERString = htmlHEADERString & "<tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td>"
        htmlHEADERString = htmlHEADERString & "<table width='100%' border='0' cellpadding='0' cellspacing='0' style=' background-color: #ff5300; BORDER-BOTTOM: #c87356 1px solid; BORDER-TOP: #c87356 1px solid; -moz-border-radius: 20px; -webkit-border-radius: 20px; -khtml-border-radius: 20px; border-radius: 20px; '>"
        htmlHEADERString = htmlHEADERString & "<tr><td valign='bottom'><table width='100%' border='0' cellspacing='0' cellpadding='25'>"
        htmlHEADERString = htmlHEADERString & "<tr><td valign='bottom'><table width='100%' border='0' cellspacing='0' cellpadding='25'>"
        htmlHEADERString = htmlHEADERString & "<font face='Arial, Helvetica, sans-serif' size='6' color='#ffffff'><strong>Jobs That May Interest You</strong></font><br></td></tr>"
        htmlHEADERString = htmlHEADERString & "</table></td></tr></table></td></tr></table></td></tr> "
        htmlHEADERString = htmlHEADERString & "<tr><td><table align='center' width='100%' border='0' cellspacing='3' cellpadding='20' style='BORDER-BOTTOM: #c87356 1px solid'>"


    End Sub

    Private Sub CreatehtmlBODYString(ByVal titlos As String, ByVal tupos As String, ByVal polh As String, ByVal misthos As String, ByVal apaithseis As String, ByVal perigrafh As String, ByVal afentiko As String, ByVal thl As String, ByVal mail As String)
        'body
        If sthlh = 0 Then
            'column 1
            htmlBODYString = htmlBODYString & "<tr><td style='background-color:#ffd7c7;'><h2>" & titlos & "</h2><p> Job Type: " & tupos & "</p><p> City: " & polh & "</p><p> Salary: " & misthos & " ευρώψ</p><p> Job Descpription: " & perigrafh & "</p><p> Job Qualifications: " & apaithseis & "</p><p> Employer: " & afentiko & "</p><p> Phone: " & thl & "</p><p> E-mail: " & mail
            htmlBODYString = htmlBODYString & "<td>"
            sthlh = sthlh + 1
        ElseIf sthlh = 1 Then
            htmlBODYString = htmlBODYString & "<td style='background-color:#ffebe3;'><h2>" & titlos & "</h2><p> Job Type: " & tupos & "</p><p> City: " & polh & "</p><p> Salary: " & misthos & "</p><p> Job Descpription: " & perigrafh & "</p><p> Job Qualifications: " & apaithseis & "</p><p> Employer: " & afentiko & "</p><p> Phone: " & thl & "</p><p> E-mail: " & mail
            htmlBODYString = htmlBODYString & "<td>"
            sthlh = sthlh + 1
        ElseIf sthlh = 2 Then
            htmlBODYString = htmlBODYString & "<tr><td style='background-color:#ffd7c7;'><h2>" & titlos & "</h2><p> Job Type: " & tupos & "</p><p> City: " & polh & "</p><p> Salary: " & misthos & "</p><p> Job Descpription: " & perigrafh & "</p><p> Job Qualifications: " & apaithseis & "</p><p> Employer: " & afentiko & "</p><p> Phone: " & thl & "</p><p> E-mail: " & mail
            htmlBODYString = htmlBODYString & "<td>"
            sthlh = sthlh + 1
        ElseIf sthlh = 3 Then
            htmlBODYString = htmlBODYString & "<td style='background-color:#ffebe3;'><h2>" & titlos & "</h2><p> Job Type: " & tupos & "</p><p> City: " & polh & "</p><p> Salary: " & misthos & "</p><p> Job Descpription: " & perigrafh & "</p><p> Job Qualifications: " & apaithseis & "</p><p> Employer: " & afentiko & "</p><p> Phone: " & thl & "</p><p> E-mail: " & mail
            htmlBODYString = htmlBODYString & "<td>"
            sthlh = 0
        End If

    End Sub

    Private Sub CreatehtmlFOOTERString()
        htmlFOOTERString = htmlFOOTERString & "</table></td></tr>"
        htmlFOOTERString = htmlFOOTERString & "<tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td>"
        htmlFOOTERString = htmlFOOTERString & "<table width='100%' border='0' cellspacing='3' cellpadding='10' style=' background-color: #ff5300; BORDER-BOTTOM: #c87356 1px solid; -moz-border-radius: 10px; -webkit-border-radius: 10px; -khtml-border-radius: 10px; border-radius: 10px;'>"
        htmlFOOTERString = htmlFOOTERString & "<tr><td valign='bottom'><table width='100%' border='0' cellspacing='0' cellpadding='0'>"
        htmlFOOTERString = htmlFOOTERString & "<tr><td align='left' cellpadding='20'><font face='Arial, Helvetica, sans-serif' size='3' color='#ffffff'>Sent By FindJobMailer</font><br> "
        htmlFOOTERString = htmlFOOTERString & "</td>"
        htmlFOOTERString = htmlFOOTERString & "</tr></table></td></tr></table></td></tr></table></td></tr></table></body></html>"




    End Sub

#End Region

    Private Sub JobsGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JobsGroup.SelectedIndexChanged
        Dim i As Integer = 0
        htmlHEADERString = ""
        htmlBODYString = ""
        htmlFOOTERString = ""
        sthlh = 0

        CreatehtmlHEADERString()
        CreatehtmlFOOTERString()


        Dim strSQL As String = "SELECT * FROM Angelia WHERE title='" & Me.JobsGroup.Text & "'"

        Try
            Using cnn As New OleDbConnection(My.Settings.OurDataBaseConnectionString)
                Using cmd As New OleDbCommand(strSQL, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()
                        ' Loop through all the rows, retrieving the 
                        ' columns you need. Also look into the GetString
                        ' method (and other Get... methods) for a faster 
                        ' way to retrieve individual columns.

                        While dr.Read
                            CreatehtmlBODYString(dr("title").ToString(), dr("types").ToString(), _
                                                 dr("city").ToString(), dr("salary").ToString(), _
                                                 dr("Qualifications").ToString(), dr("jobDescription").ToString(), _
                                                 dr("employer").ToString(), dr("phone").ToString(), dr("e-mail").ToString())


                            'ArraysentAds(i) = dr("Aid").ToString()
                            'i = i + 1
                        End While
                    End Using
                End Using
            End Using
            'Show In Web Broswer
            PreView.DocumentText = htmlHEADERString & htmlBODYString & htmlFOOTERString
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub JobsCity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JobsCity.SelectedIndexChanged

        Dim i As Integer = 0
        htmlHEADERString = ""
        htmlBODYString = ""
        htmlFOOTERString = ""
        sthlh = 0

        CreatehtmlHEADERString()
        CreatehtmlFOOTERString()


        Dim strSQL As String = "SELECT * FROM Angelia WHERE City='" & Me.JobsCity.Text & "'"

        Try
            Using cnn As New OleDbConnection(My.Settings.OurDataBaseConnectionString)
                Using cmd As New OleDbCommand(strSQL, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()
                        ' Loop through all the rows, retrieving the 
                        ' columns you need. Also look into the GetString
                        ' method (and other Get... methods) for a faster 
                        ' way to retrieve individual columns.

                        While dr.Read

                            ' ArraysentAds(i) = dr("Aid").ToString()

                            'i = i + 1

                            CreatehtmlBODYString(dr("Title").ToString(), dr("Types").ToString(), _
                                                 dr("City").ToString(), dr("Salary").ToString(), _
                                                 dr("Qualifications").ToString(), dr("jobDescription").ToString(), _
                                                 dr("Employer").ToString(), dr("Phone").ToString(), dr("e-mail").ToString())


                        End While
                    End Using
                End Using
            End Using
            'Show In Web Broswer
            PreView.DocumentText = htmlHEADERString & htmlBODYString & htmlFOOTERString
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub RecipientsGroups_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RecipientsGroups.SelectedIndexChanged
        ToText.Text = ""
        Dim i As Integer = 0
        Dim strSQL As String = "SELECT * FROM Recipients WHERE GroupName='" & Me.RecipientsGroups.Text & "'"

        Try
            Using cnn As New OleDbConnection(My.Settings.OurDataBaseConnectionString)
                Using cmd As New OleDbCommand(strSQL, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()
                        ' Loop through all the rows, retrieving the 
                        ' columns you need. Also look into the GetString
                        ' method (and other Get... methods) for a faster 
                        ' way to retrieve individual columns.

                        While dr.Read
                            If ToText.Text = "" Then
                                ToText.Text = dr("e-mail").ToString()
                                ' ArrayRecipients(i) = dr("idR").ToString()
                                ' i = i + 1
                            Else
                                ToText.Text = ToText.Text & "," & dr("e-mail").ToString()
                                ' ArrayRecipients(i) = dr("idR").ToString()
                                ' i = i + 1
                            End If

                        End While
                    End Using
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub ResetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetButton.Click
        htmlHEADERString = ""
        htmlBODYString = ""
        htmlFOOTERString = ""
        PreView.DocumentText = htmlHEADERString & htmlBODYString & htmlFOOTERString
        BodyText.Text = ""
        ToText.Text = ""
    End Sub

End Class